<?php

namespace Wap\Controller;
use Think\Controller;

class IndexController extends HomeController
{
    //拍卖首页
    public function index()
    {


        //竞价中
        $where1['end_time']=array('gt',time());
        $list   = M('bid_list')->where($where1)->select();

        //结束
        $where2['end_time']=array('lt',time());
        $list_end=M('bid_list')->where($where2)->select();


        //正在进行的所有竞价商品列表
        if(I('get.bind')){
            $map['bid_id']=I('get.bind');
            $bid=M('bid_list')->where(array('id'=> $map['bid_id']))->find();
            if($bid['end_time']<time()){
                $this->redirect('Index/area',array('bind' => $map['bid_id']));
            }
        }else{
            $list_id=array_keys(get_id_key($list,'id'));
            $map['bid_id']=array("in",$list_id);
        }
        $good_list=M('bid_good')->where($map)->select();

        //广告
        $advs=M('ad')->where(array('pid'=>1))->limit(4)->select();

        $this->assign('good_list',$good_list);

        $this->assign('advs', $advs);
        $this->assign('list', $list);
        $this->assign('list_end', $list_end);
        $this->display();
    }

  


    /**
     *拍卖结束列表页
     */
    public function area(){
        $where1['end_time']=array('gt',time());
        $list   = M('bid_list')->where($where1)->select();

        //结束
        $where2['end_time']=array('lt',time());
        $list_end=M('bid_list')->where($where2)->select();



        //正在进行的所有竞价商品列表
        if(I('get.bind')){
            $map['bid_id']=I('get.bind');
        }else{
           $list_id=array_keys(get_id_key($list_end,'id'));
           $map['bid_id']=array("in",$list_id);
        }
        $good_list=M('bid_good')->where($map)->select();

        //广告
        $advs=M('ad')->where(array('pid'=>1))->limit(4)->select();
    
        $this->assign('good_list',$good_list);

        $this->assign('advs', $advs);
        $this->assign('list', $list);
        $this->assign('list_end', $list_end);
        $this->display("area");
    }


   //详情页
   public function details(){
      //获取竞价单个产品信息
      $id=I('get.id')?I('get.id'):0;
      $bid_good=M('bid_good')->where(array('id'=>$id))->find();

      //获取竞价信息
      $bid=M('bid_list')->where(array('id'=>$bid_good['bid_id']))->find();
      if(time()<$bid['end_time']){
        $bid_good['bili']=round((time()-$bid['start_time'])/($bid['end_time']-$bid['start_time'])*100);
      }else{
        $bid_good['bili']=100;
      }

      //获取对应商品信息
      $goods=M('goods')->where(array('id'=>$bid_good['good_id']))->find();
      $goods['good_img_arr']=explode(',',$goods['good_img']);
      
      //获取对应用户报价信息
      $where['bid_id']=$bid_good['bid_id'];
      $where['good_id']=$bid_good['id'];
      $user_list=M('bid_user')->where($where)->order('per_price desc,create_time asc')->select(); 
      $bid_good['user_count']=count($user_list);
      $bid_good['max_price']=$user_list[0]['per_price'];

      $this->assign('goods', $goods);
      $this->assign('bid', $bid);
      $this->assign('bid_good', $bid_good);
      $this->assign('user_list', $user_list);

      $this->display();
   }


  public function exchange(){
    $uid=is_wxlogin();
    if(!$uid){
      $this->redirect('Public/bindUser');
    }

    //获取竞价场次中单个产品信息
    $id=I('get.id')?I('get.id'):0;
    $bid_good=M('bid_good')->where(array('id'=>$id))->find();

   //获取竞价场次信息
   $bid=M('bid_list')->where(array('id'=>$bid_good['bid_id']))->find();

    //获取对应商品信息
    $goods=M('goods')->where(array('id'=>$bid_good['good_id']))->find();
    $goods['good_img_arr']=explode(',',$goods['good_img']);

    $this->assign('goods', $goods);
    $this->assign('bid_good', $bid_good);
    $this->assign('bid', $bid);
    $this->display();
  }


  //用户竞价-专场竞价
    public function ajaxPostUserBid(){
        $data=I('post.');
        $data['user_id']=is_wxlogin();
        $data['status']=1;
        $data['create_time']=time();
        $data['user_price']=$data['user_number']*$data['per_price'];        

         $where['id']=$data['good_id'];
         $bid_good=M('bid_good')->where($where)->field('store_count')->find();
         if($data['user_number']>$bid_good['store_count']){
             $this->ajaxReturn(['status'=>0,'msg'=>'起拍数为不能超过库存']);
             die();
         }

         //查看该商品的已存在最高报价，用户报价必须高于他
         $map['bid_id']=$data['bid_id'];
         $map['good_id']=$data['good_id'];
          //每个产品的报价用户数
         $user_list=M('bid_user')->where($map)->order('per_price desc')->select();

         if(is_array($user_list) && $user_list[0]['per_rice']>=$data['per_price']){
             $this->ajaxReturn(['status'=>0,'msg'=>'本产品目前最低报价单价为'+$user_list[0]['per_rice']]);
             die();
         }

        //查看用户之前是否提交
        $map['user_id']=$data['user_id'];
        $user_bid=M('bid_user')->where($map)->field('id')->find();


        if(empty($user_bid['id'])){
            $res = M('bid_user')->add($data);
        }else{
            $res = M('bid_user')->where(array('id'=>$user_bid['id']))->save($data);
        }
       /* M('bid_good')->where($where)->setDec('store_count',$data['user_number']);*/

        if(!$res){
            $this->ajaxReturn(['status'=>0,'msg'=>'提交失败']);
        }else{
            $this->ajaxReturn(['status'=>1,'msg'=>'提交成功']);
        }
    }


   

}